package com.syn.synold4s.activity;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import androidx.activity.result.ActivityResult;
import androidx.core.content.res.ResourcesCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.gaoyi.basemodule.baseCallback.IView;
import com.gaoyi.basemodule.baseUtil.DensityUtil;
import com.gaoyi.basemodule.baseUtil.SPUtil;
import com.gaoyi.basemodule.baseUtil.SmartRefreshUtil;
import com.gaoyi.basemodule.baseUtil.ToastUtil;
import com.gaoyi.basemodule.baseUtil.WindowUtil;
import com.gaoyi.basemodule.baseact.BaseListActivity;
import com.gaoyi.basemodule.basebean.BaseQuery;
import com.gaoyi.basemodule.basebean.Event;
import com.gaoyi.basemodule.baseconstant.ApiStatus;
import com.gaoyi.basemodule.baseconstant.SPName;
import com.gaoyi.basemodule.basedialog.LoadingDialogUtils;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.syn.synold4s.R;
import com.syn.synold4s.adapter.ProjectAdapter;
import com.syn.synold4s.bean.dto.ProjectSelectionDto;
import com.syn.synold4s.control.RvFirstItemDecoration;
import com.syn.synold4s.databinding.ActivityProjectSelectionBinding;
import com.syn.synold4s.vm.ProjectSelectionViewModel;

import java.util.Map;

/**
 * 选择项目页
 */
public class ProjectSelectionActivity extends BaseListActivity<ActivityProjectSelectionBinding, ProjectSelectionViewModel, ProjectSelectionDto, BaseQuery> implements IView {

    private ProjectAdapter adapter;
    private int jumpFlag = -1;//0:选择项目页——登录页(loginToken为空)  1:设置页——选择项目页

    @Override
    protected int getLayoutId() {
        return R.layout.activity_project_selection;
    }

    @Override
    protected int initVariable() {
        return 0;
    }

    @Override
    protected void initView() {
        WindowUtil.setWindowDecorView(this);
        //单独设置字体
        Typeface typeface = ResourcesCompat.getFont(this, R.font.oswald_bold);
        mBinding.tvWelcome.setTypeface(typeface);
        //跳转标记
        jumpFlag = SPUtil.getInstance().getInt(SPName.JUMP_FLAG, -1);

        if (1 == jumpFlag) {//从设置页跳转到选择项目页
            mBinding.tvReturnLogin.setText("");
        }

        mBinding.tvReturnLogin.setOnClickListener(view -> {
            if (-1 == jumpFlag) {//回到登录页
                startActivity(new Intent(mContext, LoginActivity.class));
            }
            SPUtil.getInstance().putBoolean(SPName.TOKEN_FLAG, true);
            finish();
        });

        mBinding.rv.setLayoutManager(new LinearLayoutManager(this));
        mBinding.rv.addItemDecoration(new RvFirstItemDecoration(DensityUtil.dp2px(8)));
        adapter = new ProjectAdapter(this);
        mBinding.rv.setAdapter(adapter);


        adapter.setOnItemClickListener(((v, position) -> {
            ProjectSelectionDto positionData = adapter.getPositionData(position);
            String apiUrl = "";
            if (positionData.getApiUrl().endsWith("/")) {
                apiUrl = positionData.getApiUrl().replaceAll("/$", "");
            }
            SPUtil.getInstance().putString(SPName.API_URL, apiUrl);
            SPUtil.getInstance().putString(SPName.PROJECT_NAME, positionData.getName());
            if (1 == jumpFlag) {
                SPUtil.getInstance().putInt(SPName.JUMP_FLAG, 1);
                if (null != positionData.getLoginToken() && !TextUtils.isEmpty(positionData.getLoginToken())) {
                    mViewModel.getVerifyLogin(positionData.getLoginToken());
                } else {
                    Intent intent = new Intent(mContext, LoginActivity.class);
//                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
//                    finish();
                }
            } else {
                SPUtil.getInstance().putInt(SPName.JUMP_FLAG, 0);
                if (null != positionData.getLoginToken() && !TextUtils.isEmpty(positionData.getLoginToken())) {
                    mViewModel.getVerifyLogin(positionData.getLoginToken());
                } else {//loginToken为空
                    startActivity(new Intent(mContext, LoginActivity.class));
//                    finish();
                }
            }
        }));
    }

    @Override
    protected void initData() {
        mQuery.init();
        showLoading();
        mViewModel.getProject(mQuery);
    }

    @Override
    protected void initLiveData() {
        mViewModel.getResultCode().observe(this, integer -> SmartRefreshUtil.refreshSmartRefreshLayout(mSmartRefreshLayout));
        mViewModel.getList().observe(this, projectSelectionDtoList -> {
            if (null != projectSelectionDtoList){
                adapter.submitList(projectSelectionDtoList);
            }
        });
//        mViewModel.getVerifyLoginResultCode().observe(this, resultCode -> {
//            if (resultCode == ApiStatus.SUCCESS) {
//                SPUtil.getInstance().putBoolean(SPName.LOGIN_FLAG, true);
//                startActivity(new Intent(mContext, SurveillanceActivity.class));
//                finish();
//            }
//        });
    }

    @Override
    protected void loadData() {
        mViewModel.getProject(mQuery);
    }

    @Override
    protected BaseQuery createQuery() {
        return new BaseQuery();
    }

    @Override
    protected SmartRefreshLayout getSmartRefreshLayout() {
        return mBinding.smartRefreshLayout;
    }

    @Override
    protected void event(Event event) {

    }

    @Override
    protected void onRequestPermissionsResult(Map<String, Boolean> result) {

    }

    @Override
    protected void onActivityResult(int requestCode, ActivityResult result) {

    }

    @Override
    public void showLoading() {
        LoadingDialogUtils.getInstance().showLoading(mContext);
    }

    @Override
    public void hideLoading() {
        LoadingDialogUtils.getInstance().dismiss();
    }

    @Override
    protected void onDestroy() {
        SPUtil.getInstance().putBoolean(SPName.TOKEN_FLAG, true);
        super.onDestroy();
    }
}